import time 

from src.utils import get_logger, HeartBeat
from src.grpc_client import GrpcClient

global gd 

class GlobalDict():
    def __init__(self):
        self.logger_dict = {}

        self.lastt_dict = {}
        self.alert_dict = {}
        self.rule_info_dict = {}

        self.grpc_dict = {}

        self.heart_beat_dict = {}

        self.face_id_conf = {}

    def add_task(self, task_id, grpc_address, skip_frame_rate, add_stranger, alert_time, dbase_name):
        if task_id not in self.logger_dict.keys():
            self.logger_dict[task_id] = get_logger('./logs', f'{task_id}.log')

        self.lastt_dict[task_id] = time.time()
        self.alert_dict[task_id] = {}
        self.rule_info_dict[task_id] = {'skip_frame_rate': skip_frame_rate, 
                                        'add_stranger': add_stranger, 
                                        'alert_time': alert_time,
                                        'dbase_name': dbase_name}

        self.grpc_dict[task_id] = GrpcClient(grpc_address, task_id) if grpc_address else None

        self.heart_beat_dict[task_id] = HeartBeat(task_id)

    def remove_task(self, task_id):
        if task_id in self.lastt_dict.keys(): self.lastt_dict.pop(task_id)
        if task_id in self.alert_dict.keys(): self.alert_dict.pop(task_id)
        if task_id in self.rule_info_dict.keys(): self.rule_info_dict.pop(task_id)
        
        if task_id in self.grpc_dict.keys(): self.grpc_dict.pop(task_id)

        if task_id in self.heart_beat_dict.keys(): self.heart_beat_dict.pop(task_id)

    def update_task(self, task_id, rule_info):
        if task_id in self.rule_info_dict.keys():
            for k, v in rule_info.items():
                self.rule_info_dict[task_id][k] = v 
        

gd = GlobalDict()
